/*
 *------------------------------------------------------------------------------
 * @File      :    af_customer.c
 * @Date      :    2021-8-28
 * @Author    :    lomboswer <lomboswer@lombotech.com>
 * @Brief     :    Source file for MDP(Media Development Platform).
 *
 * Copyright (C) 2020-2021, LomboTech Co.Ltd. All rights reserved.
 *------------------------------------------------------------------------------
 */

#include "ei_posix.h"

#include "mi_isp.h"
#include "af_customer.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */

AF_CUSTOMER_CTX_S g_astAfCustomerCtx[MAX_AF_LIB_NUM] = {0};

EI_S32 AF_CUSTOMER_Init(EI_S32 s32Id, const ISP_AF_PARAM_S *pstAfParam)
{
    AF_CUSTOMER_CTX_S *pstAfCtx = EI_NULL;

    AF_CUSTOMER_CHECK_ID(s32Id);
    pstAfCtx = AF_CUSTOMER_GET_CTX(s32Id);

    AF_CUSTOMER_CHECK_POINTER(pstAfParam);

    memcpy(&pstAfCtx->stAfParam, pstAfParam, sizeof(ISP_AF_PARAM_S));

    pstAfCtx->u32Reseted = 0;

    //TODO

    return EI_SUCCESS;
}

EI_S32 AF_CUSTOMER_Exec(EI_S32 s32Id, const ISP_AF_INFO_S *pstAfInfo, ISP_AF_RESULT_S *pstAfResult)
{
    AF_CUSTOMER_CTX_S *pstAfCtx = EI_NULL;
    EI_U32 u32SkipFrames = 2;

    AF_CUSTOMER_CHECK_ID(s32Id);
    pstAfCtx = AF_CUSTOMER_GET_CTX(s32Id);

    AF_CUSTOMER_CHECK_POINTER(pstAfInfo);
    AF_CUSTOMER_CHECK_POINTER(pstAfResult);

    AF_CUSTOMER_CHECK_POINTER(pstAfInfo->pstAfStat);

    pstAfCtx->u32FrameCnt = pstAfInfo->u32FrameCnt;

    if (0 == pstAfCtx->u32FrameCnt % u32SkipFrames) {
        memcpy(&pstAfCtx->stAfInfo, pstAfInfo, sizeof(ISP_AF_INFO_S));
        EI_U64 u64Fv = 0;
        EI_U64 u64FvR = 0;
        EI_U32 i = 0;
        for (i = 0; i < ISP_MAX_METER_ZONES; i++) {
            u64Fv += pstAfInfo->pstAfStat->u64Sharp[i];
            u64FvR += pstAfInfo->pstAfStat->u32SharpReliablility[i];
        }
        u64Fv = u64Fv / ISP_MAX_METER_ZONES;
        u64FvR = u64FvR / ISP_MAX_METER_ZONES;
        printf("FrameCnt = %d, FV = %lld %lld\n", pstAfCtx->u32FrameCnt, u64Fv, u64FvR);
        //TODO
    }

    return EI_SUCCESS;
}

EI_S32 AF_CUSTOMER_Ctrl(EI_S32 s32Id, EI_U32 u32Cmd, EI_VOID *pValue)
{
    AF_CUSTOMER_CTX_S *pstAfCtx = EI_NULL;
    AF_CUSTOMER_CHECK_ID(s32Id);
    AF_CUSTOMER_CHECK_POINTER(pValue);

    pstAfCtx = AF_CUSTOMER_GET_CTX(s32Id);
    pstAfCtx = pstAfCtx;

    switch (u32Cmd) {
    default :
        break;
    }

    return EI_SUCCESS;
}

EI_S32 AF_CUSTOMER_Exit(EI_S32 s32Id)
{
    AF_CUSTOMER_CTX_S *pstAfCtx = EI_NULL;

    AF_CUSTOMER_CHECK_ID(s32Id);
    pstAfCtx = AF_CUSTOMER_GET_CTX(s32Id);

    pstAfCtx->u32Reseted = 1;

    //TODO

    return EI_SUCCESS;
}

EI_S32 EI_MI_AF_CUSTOMER_Register(ISP_DEV dev, ISP_3ALIB_S *pstAfLib)
{
    ISP_AF_REGISTER_S stRegister;
    EI_S32 s32Ret = EI_SUCCESS;
    EI_S32 s32Id;
    AF_CUSTOMER_CTX_S *pstAfCtx = EI_NULL;

    AF_CUSTOMER_CHECK_DEV(dev);
    AF_CUSTOMER_CHECK_POINTER(pstAfLib);
    AF_CUSTOMER_CHECK_ID(pstAfLib->s32Id);
    AF_CUSTOMER_CHECK_LIB_NAME(pstAfLib->acLibName);
    s32Id = pstAfLib->s32Id;
    pstAfCtx = AF_CUSTOMER_GET_CTX(s32Id);
    pstAfCtx->IspBindDev = dev;

    stRegister.stAfFunc.pfn_af_init = AF_CUSTOMER_Init;
    stRegister.stAfFunc.pfn_af_exec = AF_CUSTOMER_Exec;
    stRegister.stAfFunc.pfn_af_ctrl = AF_CUSTOMER_Ctrl;
    stRegister.stAfFunc.pfn_af_exit = AF_CUSTOMER_Exit;
    s32Ret = EI_MI_ISP_AFLibRegCallBack(dev, pstAfLib, &stRegister);
    if (EI_SUCCESS != s32Ret) {
        AF_CUSTOMER_TRACE(EI_DBG_ERR, "af register failed!\n");
    }

    return s32Ret;
}

EI_S32 EI_MI_AF_CUSTOMER_UnRegister(ISP_DEV dev, ISP_3ALIB_S *pstAfLib)
{
    EI_S32 s32Ret = EI_SUCCESS;

    AF_CUSTOMER_CHECK_DEV(dev);
    AF_CUSTOMER_CHECK_POINTER(pstAfLib);
    AF_CUSTOMER_CHECK_ID(pstAfLib->s32Id);
    AF_CUSTOMER_CHECK_LIB_NAME(pstAfLib->acLibName);

    s32Ret = EI_MI_ISP_AFLibUnRegCallBack(dev, pstAfLib);
    if (EI_SUCCESS != s32Ret) {
        AF_CUSTOMER_TRACE(EI_DBG_ERR, "ae unregister failed!\n");
    }

    return s32Ret;
}

EI_S32 EI_MI_AF_CUSTOMER_DriverRegCallBack(ISP_DEV dev, ISP_3ALIB_S *pstAfLib, AF_CUSTOMER_DRIVER_REGISTER_S *pstRegister)
{
    AF_CUSTOMER_CTX_S *pstAfCtx = EI_NULL;
    EI_S32 s32Id;

    AF_CUSTOMER_CHECK_DEV(dev);
    AF_CUSTOMER_CHECK_POINTER(pstAfLib);
    AF_CUSTOMER_CHECK_POINTER(pstRegister);

    s32Id = pstAfLib->s32Id;
    AF_CUSTOMER_CHECK_ID(s32Id);
    AF_CUSTOMER_CHECK_LIB_NAME(pstAfLib->acLibName);

    pstAfCtx = AF_CUSTOMER_GET_CTX(s32Id);

    memcpy(&pstAfCtx->stAfRegister, pstRegister, sizeof(AF_CUSTOMER_DRIVER_REGISTER_S));
    pstAfCtx->bAfDrvRegister = EI_TRUE;

    return EI_SUCCESS;
}

EI_S32 EI_MI_AF_CUSTOMER_DriverUnRegCallBack(ISP_DEV dev, ISP_3ALIB_S *pstAfLib, SENSOR_ID SnsId)
{
    AF_CUSTOMER_CTX_S *pstAfCtx = EI_NULL;
    EI_S32 s32Id;

    AF_CUSTOMER_CHECK_DEV(dev);
    AF_CUSTOMER_CHECK_POINTER(pstAfLib);

    s32Id = pstAfLib->s32Id;
    AF_CUSTOMER_CHECK_ID(s32Id);
    AF_CUSTOMER_CHECK_LIB_NAME(pstAfLib->acLibName);

    pstAfCtx = AF_CUSTOMER_GET_CTX(s32Id);

    memset(&pstAfCtx->stAfRegister, 0, sizeof(AF_CUSTOMER_DRIVER_REGISTER_S));
    pstAfCtx->bAfDrvRegister = EI_FALSE;

    return EI_SUCCESS;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
